package com.ihd.ihardware.view.tzc.discovery.model;

import android.os.Bundle;
import android.text.TextUtils;

import com.google.gson.JsonObject;
import com.ihd.ihardware.api.HttpClient;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.pojo.ArticalRes;
import com.ihd.ihardware.pojo.CommentRes;
import com.ihd.ihardware.pojo.CommentsRes;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.FileRes;
import com.ihd.ihardware.pojo.MsgRes;
import com.ihd.ihardware.pojo.MsgsRes;
import com.ihd.ihardware.pojo.RecommendRes;
import com.ihd.ihardware.pojo.SceneRes;
import com.ihd.ihardware.pojo.SignsRes;

import org.json.JSONArray;
import org.json.JSONObject;
import org.reactivestreams.Publisher;

import java.io.File;

import cn.wowjoy.commonlibrary.http.BaseSubscriber;
import cn.wowjoy.commonlibrary.rxbus.RxBus;
import cn.wowjoy.commonlibrary.utils.RxSchedulerTransformer;
import cn.wowjoy.commonlibrary.viewmodel.BaseRepository;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class DiscoveryRepository extends BaseRepository {
    public void getConcernList() {
        addDisposable(HttpClient.getService().recommend()
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<RecommendRes>(AppConstans.RECOMMEND) {
                }));
    }

    public void getDiscoveryList(int pageNum, int pageSize) {
        addDisposable(HttpClient.getService().concernSigns(pageNum, pageSize)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<SignsRes>(AppConstans.CONCERN_SIGNS) {
                }));
    }

    public void getZJList() {
        addDisposable(HttpClient.getService().queryListScene(1)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<SceneRes>(AppConstans.ZJ) {
                }));
    }

    public void getMJList() {
        addDisposable(HttpClient.getService().queryListScene(2)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<SceneRes>(AppConstans.MJ) {
                }));
    }

    public void getJXMJList() {
        addDisposable(HttpClient.getService().queryListScene(3)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<SceneRes>(AppConstans.JXMJ) {
                }));
    }

    public void getDTList(int pageNum, int pageSize) {
        addDisposable(HttpClient.getService().signs(pageNum, pageSize)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<SignsRes>(AppConstans.SIGNS) {
                }));
    }

    public void getCollectList(int pageNum, int pageSize, String category) {
        if (TextUtils.isEmpty(category))
            addDisposable(HttpClient.getService().queryList(pageNum, pageSize, category)
                    .compose(new RxSchedulerTransformer<>())
                    .subscribeWith(new BaseSubscriber<ArticalRes>(AppConstans.ARTS) {
                    }));
        else
            addDisposable(HttpClient.getService().queryList(pageNum, pageSize, category)
                    .compose(new RxSchedulerTransformer<>())
                    .subscribeWith(new BaseSubscriber<ArticalRes>(AppConstans.ARTS_SUB) {
                    }));
    }

    public void tag(String signId) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("signId", signId);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().tag(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribe(new Consumer<EmptyRes>() {
                    @Override
                    public void accept(EmptyRes emptyRes) throws Exception {
                        Bundle b = new Bundle();
                        b.putInt("key", emptyRes.getCode());
                        b.putInt("type", 1);
                        b.putString("value", emptyRes.getCode() == 0 ? signId : emptyRes.getMessage());
                        RxBus.getInstance().post(AppConstans.TAG, b);
                    }
                })
        );
    }

    public void delTag(String signId) {
        addDisposable(HttpClient.getService().delTag(signId)
                .compose(new RxSchedulerTransformer<>())
                .subscribe(new Consumer<EmptyRes>() {
                    @Override
                    public void accept(EmptyRes emptyRes) throws Exception {
                        Bundle b = new Bundle();
                        b.putInt("key", emptyRes.getCode());
                        b.putInt("type", 0);
                        b.putString("value", emptyRes.getCode() == 0 ? signId : emptyRes.getMessage());
                        RxBus.getInstance().post(AppConstans.TAG, b);
                    }
                })
        );
    }

    public void concern(String concernUserId) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("concernUserId", concernUserId);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().concern(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribe(new Consumer<EmptyRes>() {
                    @Override
                    public void accept(EmptyRes emptyRes) throws Exception {
                        Bundle b = new Bundle();
                        b.putInt("key", emptyRes.getCode());
                        b.putInt("type", 1);
                        b.putString("value", emptyRes.getCode() == 0 ? concernUserId : emptyRes.getMessage());
                        RxBus.getInstance().post(AppConstans.CONCERN, b);
                    }
                }));
    }

    public void delConcern(String concernUserId) {
        addDisposable(HttpClient.getService().delConcern(concernUserId)
                .compose(new RxSchedulerTransformer<>())
                .subscribe(new Consumer<EmptyRes>() {
                    @Override
                    public void accept(EmptyRes emptyRes) throws Exception {
                        Bundle b = new Bundle();
                        b.putInt("key", emptyRes.getCode());
                        b.putInt("type", 0);
                        b.putString("value", emptyRes.getCode() == 0 ? concernUserId : emptyRes.getMessage());
                        RxBus.getInstance().post(AppConstans.CONCERN, b);
                    }
                }));
    }

    public void updateHead(File file) {
        MultipartBody.Part requestBody = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
        addDisposable(HttpClient.getService().uploadToOss(requestBody)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<FileRes>(AppConstans.UPDATE) {
                }));
    }

    public void sign(String fileId, String textShow) {
        try {
            JSONArray jsonArray = new JSONArray();
            jsonArray.put(fileId);
            JSONObject mParam = new JSONObject();
            mParam.put("fileIds", jsonArray);
            mParam.put("textShow", textShow);
            RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
            addDisposable(HttpClient.getService().sign(rb)
                    .compose(new RxSchedulerTransformer<>())
                    .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.PUBLISH) {
                    }));
        } catch (Exception e) {

        }
    }

    public void getComments(int pageNum, int pageSize, String signId) {
        addDisposable(HttpClient.getService().getComments(pageNum, pageSize, signId)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<CommentsRes>(AppConstans.COMMENTS) {
                }));
    }

    public void articleCommentList(int pageNum, int pageSize, String articleId) {
        addDisposable(HttpClient.getService().articleCommentList(pageNum, pageSize, articleId)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<CommentRes>(AppConstans.ARTCOMMENTS) {
                }));
    }

    public void sendComment(String signId, String commentsText) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("signId", signId);
        mParam.addProperty("commentsText", commentsText);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().sendComment(rb)
                .flatMap(new Function<EmptyRes, Publisher<CommentsRes>>() {
                    @Override
                    public Publisher<CommentsRes> apply(EmptyRes emptyRes) throws Exception {
                        return HttpClient.getService().getComments(1, 100, signId);
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<CommentsRes>(AppConstans.SENDCOMMENT) {
                }));
    }

    public void sendArtComment(String id, String content) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("id", id);
        mParam.addProperty("content", content);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().sendArtComment(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.SENDARTCOMMENT) {
                }));
    }

    public void artCollect(String id) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("id", id);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().artCollect(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.ARTCOLLECT) {
                }));
    }

    public void collectCancel(String id) {
        addDisposable(HttpClient.getService().collectCancel(id)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.ARTCOLLECTCANCEL) {
                }));
    }

    public void unReadMsgs() {
        addDisposable(HttpClient.getService().unReadMsgs()
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<MsgRes>(AppConstans.UNREADMSG) {
                }));
    }

    public void msgs(String msgType) {
        addDisposable(HttpClient.getService().msgs(msgType)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<MsgsRes>(AppConstans.MSGS) {
                }));
    }
}
